package com.example.jdbi.dao;

import com.example.jdbi.config.JdbiMapper;
import com.example.jdbi.entity.User;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.UseRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * <pre>
 * 第 1 种自定义 findByJpql
 * 第 2 种自定义 findCustomerBySpec
 * 第 3 种自定义 @see UserExtOperation # findExtends
 * </pre>
 *
 * @author fanhang
 */
@JdbiMapper
public interface UserMapper {

    /**
     * 第 1 种自定义
     */
    @SqlQuery("select * from t_user where age = :age and name like '%:name%' ")
    @UseRowMapper(UserRowMapper.class)
    List<User> findByAgeLikeName(int age, String name);


    class UserRowMapper implements RowMapper<User> {
        @Override
        public User map(ResultSet rs, StatementContext ctx) throws SQLException {
            User user = new User();
            user.setId(rs.getLong("id"));
            user.setName(rs.getString("name"));
            user.setAge(rs.getInt("age"));
            user.setRemark(rs.getString("remark"));
            return user;
        }
    }
}
